
local ZERO_DISTANCE = 8 


local i26wendu = Class(function(self, inst)
    self.inst = inst

    self.total = 100
    self.current = 50

    self.xzwytask = inst:DoPeriodicTask(3, function()
        self:RtFn()
    end)
end)

function i26wendu:RtFn()
    if self.inst:HasTag("flying") then return end --飞行时不吸收

    local x, y, z = self.inst.Transform:GetWorldPosition()
    local owner = self.inst.components.inventoryitem:GetGrandOwner()

    local delta = 0
    if not owner then  --不带在身上才会吸收
        local ents = TheSim:FindEntities(x, y, z, ZERO_DISTANCE, {"HASHEATER"}, {"INLIMBO"})
        for k, v in pairs(ents) do
            if v ~= self.inst and
            not v:IsInLimbo() and
            v.components.heater ~= nil then
                if v.components.heater:IsExothermic() then
                    delta = delta + 5
                elseif v.components.heater:IsEndothermic() then
                    delta = delta - 5
                end
            end
        end
        delta = math.clamp(delta, -10, 10)
    end 

    if self.current ~= 50 then
        if self.current > 50 then
            local tartemp = 50
            self.inst.components.heater.heat = tartemp
            self.inst.components.heater.equippedheat = tartemp
            self.inst.components.heater.carriedheat = tartemp
            self.inst.components.heater:SetThermics(true, false)

            if owner and owner:HasTag("player") then
                owner.i26wendu_min = self.inst
                if owner.i26wendu_max == self.inst then
                    owner.i26wendu_max = nil
                end
            end
            delta = delta - .1
        else
            local tartemp = 20
            self.inst.components.heater.heat = tartemp
            self.inst.components.heater.equippedheat = tartemp
            self.inst.components.heater.carriedheat = tartemp
            self.inst.components.heater:SetThermics(false, true)

            if owner and owner:HasTag("player") then
                owner.i26wendu_max = self.inst
                if owner.i26wendu_min == self.inst then
                    owner.i26wendu_min = nil
                end
            end
            delta = delta + .1
        end
    else
        self.inst.components.heater:SetThermics(false, false)
        if owner and owner:HasTag("player") then
            if owner.i26wendu_max == self.inst then
                owner.i26wendu_max = nil
            end
            if owner.i26wendu_min == self.inst then
                owner.i26wendu_min = nil
            end
        end
        
    end


    self:SetUses(self.current + delta)
end

function i26wendu:GetPercent()
    return self.current / self.total
end

function i26wendu:SetUses(val)
    self.current = math.clamp(val, 0, self.total)
    self.inst:PushEvent("i26wenduchange", {lv = self:GetLv()})

    self:Clight()
    self:CIandA()
    -- if self.inst.components.talker then
    --     self.inst.components.talker:Say(self.current..".")
    -- end
    if self.ddfn ~= nil then
        self:ddfn(self.current)
    end
end

function i26wendu:CIandA()
    local lv = self:GetLv()
    if not self.inst:HasTag("flying") then
        self.inst.AnimState:PlayAnimation("ground_"..lv)
    end
    self.inst.components.inventoryitem:ChangeImageName("i26_laotie_"..lv)
end

function i26wendu:Clight()
    local lv = self:GetLv()
    if lv == 0 then
        self.inst.Light:Enable(false)
    else
        self.inst.Light:Enable(true)
    end
end

function i26wendu:GetLv()
    local ego = self.inst.components.i26egotool
    if self.current < 47 then
        return math.min(2, ego.unlock_cold)
    elseif self.current < 49.2 then
        return math.min(1, ego.unlock_cold)
    elseif self.current > 53 then
        if ego.unlock_hot == 2 then
            return 0
        end
        return math.min(4, ego.unlock_hot)
    elseif self.current > 50.8 then
        return ego.unlock_hot == 2 and 0 or 3
    else
        return 0
    end
end

function i26wendu:OnSave()
    return {
        cur = self.current
    }
end

function i26wendu:OnLoad(data)
    if data.cur ~= nil then
        self:SetUses(data.cur)
    end
    
end


return i26wendu